//
//  DetailController.m
//  CMJWeibo
//
//  Created by BH on 16/3/8.
//
//

#import "DetailController.h"
#import <Masonry/Masonry.h>
#import "StatusToolBar.h"
#import "HomeCell.h"
#import "DetailCell.h"
#import "CMJCommon.h"
#import "UIView+MJ.h"
#import "StatusModel.h"
#import "StatusTool.h"
#import "DetailStatusParam.h"
#import "ComposeController.h"
#import "CMJNavgationController.h"

@interface ButtonTitleLabel : UIButton

@end

@implementation ButtonTitleLabel

-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width;
    CGFloat titleH = self.height*0.9;
    
    return CGRectMake(titleX, titleY, titleW, titleH);
}

@end

@interface DetailController ()<UITableViewDataSource,UITableViewDelegate,StatusToolBarDelegate>
{
    NSInteger selectedBtnTag;
}

@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,strong)NSArray *commentArr;//评论数组

@end

@implementation DetailController
-(id)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏
    self.title = @"微博正文";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"......" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonClick:)];
    
    //添加底部工具栏
    StatusToolBar *toolbar = [[StatusToolBar alloc] init];
    toolbar.delegate = self;
    [self.view addSubview:toolbar];
    
    [toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
    //设置tableview
    [self setTableView];
    
    //获取评论列表
    [self getCommentList];
}

/** 设置tableview */
-(void)setTableView{
    //设置tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = GlobalBGColor;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(@(self.navigationController.navigationBar.height));
        make.bottom.equalTo(self.view).offset(-44);
    }];
}

/** rightBarButtonItem点击事件 */
-(void)rightBarButtonClick:(id)sender{
    NSLog(@"点击了右边按钮");
}

#pragma mark - tableview datasource & delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return self.commentArr.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeCell *cell = [HomeCell cellWithTableView:tableView];
        cell.statusF = self.statusF;
        return cell;
    }else{
        DetailCell *cell = [DetailCell cellWithTableView:tableView];
        cell.detailM = self.commentArr[indexPath.row];
        
        return cell;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return [self setSectionHeaderView];
    }else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        return self.statusF.cellHeight;
    }else{
        DetailModel *model = self.commentArr[indexPath.row];
        DetailCell *cell = [[DetailCell alloc] init];
        return [cell height:model];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

/** tableview section headerview */
-(UIView *)setSectionHeaderView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
    view.backgroundColor = [UIColor whiteColor];
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, view.height-1, view.width, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    //转发
    UIButton *repostBtn = [self addBtnWithTitle:[NSString stringWithFormat:@"转发%ld",self.statusF.status.reposts_count] frame:CGRectMake(0, 0, view.width*0.2, view.height-1)];
    repostBtn.tag = 101;
    [view addSubview:repostBtn];
    //评论
    UIButton *commentBtn = [self addBtnWithTitle:[NSString stringWithFormat:@"评论%ld",self.statusF.status.comments_count] frame:CGRectMake(CGRectGetMaxX(repostBtn.frame), 0, repostBtn.width, view.height-1)];
    commentBtn.tag = 102;
    commentBtn.selected = YES;
    commentBtn.backgroundColor = [UIColor orangeColor];
    selectedBtnTag = commentBtn.tag;
    [view addSubview:commentBtn];
    //nothing
    UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(commentBtn.frame), 0, view.width*0.4, view.height-1)];
    spaceView.backgroundColor = [UIColor whiteColor];
    [view addSubview:spaceView];
    //赞
    UIButton *zanBtn = [self addBtnWithTitle:[NSString stringWithFormat:@"赞%ld",self.statusF.status.attitudes_count] frame:CGRectMake(CGRectGetMaxX(spaceView.frame), 0, repostBtn.width, view.height-1)];
    zanBtn.tag = 103;
    [view addSubview:zanBtn];
    
    return view;
}

/** 设置转发、评论、赞按钮 */
-(UIButton *)addBtnWithTitle:(NSString *)title frame:(CGRect)frame
{
    ButtonTitleLabel *btn = [ButtonTitleLabel buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.frame = frame;
    btn.titleLabel.font = [UIFont systemFontOfSize:15.f];
    btn.titleLabel.backgroundColor = [UIColor whiteColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(sectionHeaderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

//sectionheader-转发、评论、赞按钮点击事件
-(void)sectionHeaderBtnClick:(UIButton *)button
{
    //取消之前选中的按钮
    if (button.tag != selectedBtnTag) {
        UIButton *lastBtn = [self.view viewWithTag:selectedBtnTag];
        lastBtn.selected = NO;
        lastBtn.backgroundColor = [UIColor whiteColor];
        selectedBtnTag = button.tag;
    }
    //选中当前按钮
    if (!button.selected) {
        button.selected = YES;
        button.backgroundColor = [UIColor orangeColor];
    }
    
}

/** 获取微博评论列表 */
-(void)getCommentList
{
    self.commentArr = [NSArray array];
    
    DetailStatusParam *param = [DetailStatusParam param];
    param.id = @(self.statusF.status.idstr.longLongValue);
    
    [StatusTool getStatusCommentWithParam:param success:^(DetailStatusResult *result) {
        self.commentArr = result.comments;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - StatusToolBarDelegate
-(void)statusToolBar:(StatusToolBar *)toolBar buttonTag:(NSInteger)tag
{
    if (tag == 202) {
        [self createComment];
    }
}

/** 发布评论 */
-(void)createComment
{
    ComposeController *compose = [[ComposeController alloc] init];
    compose.type = ComposeTypeComment;
    CMJNavgationController *navCtrl = [[CMJNavgationController alloc] initWithRootViewController:compose];
    [self presentViewController:navCtrl animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
